
package com.showcai.service.impl;

import com.showcai.module.ApplyWatch;
import com.showcai.module.Order;
import com.showcai.module.OrderItem;
import com.showcai.module.Payment;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by baggio on 2017/1/1.
 */
public class PayBase extends ServiceBase {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private UserService userService;
    @Autowired
    private ApplyService applyWatchService;
     /*  @Autowired
      private DistributorOrderService distributorOrderService;
      @Autowired
      private CommissionService commissionService;*/
    /**
     * 微信支付通知状态： 成功 SUCCESS
     */
    protected static final String WECHAT_PAY_NOTTIFY_STATUS_SUCCESS = "SUCCESS";
    /**
     * 微信支付应答状态：成功 OK
     */
    protected static final String WECHAT_PAY_NOTTIFY_RETURN_MSG_OK = "OK";
    /**
     * 微信支付状态：失败 FAIL
     */
    protected static final String WECHAT_PAY_NOTTIFY_STATUS_FAIL = "FAIL";

    protected void payin(Long orderId, String acqSeqId, Byte payMode) {
        logger.info("【payIN===》》》】");
        Order order = orderService.getOrderById(orderId);
        order.setStatus(OrderService.PAYED);
        for (OrderItem orderItem : order.getOrderItemList()) {
            if (orderItem.getStatus() == OrderService.CANCEL) {
                logger.info("项目已经去取消，跳出循环。。。。。");
                continue;
            }
            orderItem.setStatus(OrderService.WAIT_DISPATCH);
            orderItem.setCreateTime(new Date());
            orderItemService.modifyOrderItem(orderItem);
            if (orderItem.getStatus() == OrderService.COMPELTEED) {
                orderItemService.completeOrderItem(orderItem.getOrderItemId());
            }
            logger.info("支付成功，较少数量");

            // productEntityService.reduceProductEntity(productEntityId);


        }
        for (Payment payment : order.getPaymentList()
                ) {
            if (payMode == payment.getPayMode()) {
                payment.setStatus(OrderService.PAYED);
                payment.setCreateTime(new Date());
                payment.setExtOrderId(acqSeqId);
                paymentService.modifyPayment(payment);
            }
        }
        orderService.modifyOrder(order);

        ApplyWatch applyWatch = (ApplyWatch) applyWatchService.selectApplyByOrderId(orderId);
        if (applyWatch != null) {
            applyWatch.setStatus(OrderService.PAYED);
            applyWatchService.modifyApply(applyWatch);
        }
      /*  commissionService.commission(order);
        distributorOrderService.insertDistributorOrder(order);*/
    }


    protected String getProductName(String orderId) {

        Order order = orderService.getOrderById(Long.parseLong(orderId));
        if (order == null) {
            throw new MyRunTimeExcption("订单不存在，支付失败！");
        }
        //商户订单号，商户网站订单系统中唯一订单号，必填


        //订单名称，必填
        StringBuffer productName = new StringBuffer();
        List<OrderItem> orderItemList = orderItemService.getOrderItemByOrderId(Long.parseLong(orderId));

        for (OrderItem orderItem : orderItemList) {
            productName.append(orderItem.getProductEntity().getProductEntityName());

        }
        return productName.toString();
    }


}
